<script setup lang="ts">
import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import {getCurrentUser} from "../../services/userServices";

const user = ref();
const router = useRouter();
const toEdit = (editKey: string, editName: string, currentValue: string) => {
  router.push({
    path: '/user/edit',
    query: {
      editKey,
      editName,
      currentValue,
    }
  })
}

onMounted(async ()=> {
  user.value = await getCurrentUser();
})

function formatDate(dateString: string) {
  const date = new Date(dateString);
  const year = date.getFullYear();
  const month = padZero(date.getMonth() + 1);
  const day = padZero(date.getDate());
  const hour = padZero(date.getHours());
  const minute = padZero(date.getMinutes());
  const second = padZero(date.getSeconds());
  return `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
}

function padZero(num: number) {
  return num.toString().padStart(2, '0');
}

</script>

<template>
  <div v-if="user">
    <van-cell title="编号" :value="user.idCode" />
    <van-cell title="账号" :value="user.userAccount" />
    <van-cell title="头像" is-link to="/user/edit" @click="toEdit('avatarUrl', '头像地址', user.avatarUrl)">
      <img style="height: 47px" :src="user.avatarUrl" alt=""/>
    </van-cell>
    <van-cell title="用户名" is-link to="/user/edit" :value="user.username" @click="toEdit('username', '用户名', user.username)" />
    <van-cell title="性别" is-link to="/user/edit" :value="user.gender === 1 ? '男' : '女'" @click="toEdit('gender', '性别', user.gender === 1 ? '男' : '女')" />
    <van-cell title="电话" is-link to="/user/edit" :value="user.phone" @click="toEdit('phone', '电话', user.phone)" />
    <van-cell title="邮箱" is-link to="/user/edit" :value="user.email" @click="toEdit('email', '邮箱', user.email)" />
    <van-cell title="创建时间" :value="formatDate(user.createTime)" />
  </div>


</template>

<style scoped>

</style>